package eppic.pkg;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import android.app.*;
import android.content.*;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.TimePicker;
import android.os.Handler;
import android.view.View.OnClickListener;
import java.text.SimpleDateFormat;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Calendar;
import android.app.TimePickerDialog;
import java.util.Date;
import java.sql.Timestamp;
import java.text.DateFormat;


public class EnterExpensesActivity extends Activity {
	
	public String project_name;
	public float rate;
	public int is_project;
	public int parent_id;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.enterexpenses);
        Bundle extras = getIntent().getExtras();
        
        //Get Type and set Name
        int id = extras.getInt("client_id");
        TextView task_name = (TextView) findViewById(R.id.task);
        
        if(id != 0)
        {
        	Client client = new Client(id, this);
        	project_name = client.company;
        	rate = client.rate;
        	parent_id = id;
        	is_project = 0;
        }
        else
        {
        	id = extras.getInt("project_id");
        	Project project = new Project(id, this);
        	project_name = project.name;
        	rate = project.rate;
        	parent_id = id;
        	is_project = 1;
        }
        task_name.setText(project_name);
        
        
        //Do Rate
        EditText cost = (EditText) findViewById(R.id.cost);
        cost.setText("$0.00");
        rateTextWatcher rateWatcher = new rateTextWatcher(this, cost);
        cost.addTextChangedListener(rateWatcher);
        
    }
    
    public void submitExpense(View view)
    {
    	//name
    	String name = ((EditText) findViewById(R.id.expense_name)).getText().toString();
    	
    	//cost
    	int string_start = 0;
    	String rate_string = ((EditText) findViewById(R.id.cost)).getText().toString();
    	if(rate_string.substring(0,1).equals("$"))
    		string_start = 1;
    	float cost = new Float(rate_string.substring(string_start, rate_string.length()));
    	
    	
    	Expense expense = new Expense(parent_id, is_project, name, cost);
    	
    	if(expense.save(getApplicationContext()))
    	{
    		Toast.makeText(getApplicationContext(), "Expense Added", Toast.LENGTH_SHORT).show();
    		finish();
    	}
    	else
    	{
    		Toast.makeText(getApplicationContext(), "Failed to add Expense. Try Again."+cost, Toast.LENGTH_SHORT).show();
    	}

    	
    }
    
    public void goBack(View view) {
    	finish();
    }
    
}